/* This is a panel that holds the weapons
 * 
 */

package gui.battle;

import gui.utilities.*;
import item.ship.*;
import item.weapon.*;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class WeaponsPanel extends JPanel {

	ArrayList<JPanel> buttons; //the up to five weapon buttons
	ArrayList<Weapon> weapons;

	//1 param constructor that sets up the panel
	public WeaponsPanel(Ship s){


		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		this.setOpaque(false);

		String normalButtonPath = "assets\\images\\buttonImages\\normal\\weapon_background.png";
		String hoverButtonPath = "assets\\images\\buttonImages\\hover\\weapon_background.png";
		String selectButtonPath = "assets\\images\\buttonImages\\select\\weapon_background.png";

		this.setMaximumSize(new Dimension(600, 100));

		weapons = s.getWeapon();
		buttons = new ArrayList<JPanel>(weapons.size());

		if(s.equals(BattlePanel.turn.getShip())){

			for(int i = 0; i < weapons.size(); i++){

				buttons.add(new MySelectableButton(normalButtonPath, hoverButtonPath, selectButtonPath, 120, 100));
				buttons.get(i).addMouseListener(new MouseAdapter(){

					public void mouseClicked(MouseEvent e){

						if(((MySelectableButton)e.getComponent()).getSelected()){

							BattlePanel.selectedWeapon = null;
							((MySelectableButton)e.getComponent()).deselectButton();

							return;
						}//if


						for(JPanel b: buttons){

							((MySelectableButton)b).deselectButton();
						}//for

						((MySelectableButton) e.getComponent()).selectButton();

						BattlePanel.selectedWeapon = weapons.get(buttons.indexOf(e.getComponent()));
					}//mouseClicked
				});
			}//for
		}//if
		
		else{
			
			for(int i = 0; i < weapons.size(); i++){
				
				buttons.add(new MySelectableButton(normalButtonPath, normalButtonPath, normalButtonPath, 120, 100));
				
			}//for
		}//else
		

		JLayeredPane weaponButton; //the buttons for the weapons

		for(int i = 0; i < weapons.size(); i++){

			weaponButton = new JLayeredPane(); //the complete button
			weaponButton.setMaximumSize(new Dimension(120, 100));
			weaponButton.setLayout(new OverlayLayout(weaponButton) {

				public void layoutContainer(Container target) {
					for (Component c: target.getComponents())
						c.setBounds(0, 0, target.getWidth(), target.getHeight());
				}
			});


			JPanel txt = new JPanel(); //the text over the button
			txt.setOpaque(false);
			txt.setLayout(new BoxLayout(txt, BoxLayout.X_AXIS));

			JPanel spacing = new JPanel(); //adds a buffer to the left
			spacing.setOpaque(false);
			spacing.setMaximumSize(new Dimension(10,0));

			JPanel p = new JPanel(); //actually contains the text
			p.setMaximumSize(new Dimension(120, 100));
			p.setOpaque(false);
			p.setLayout(new GridLayout(5, 1, 0, 0));

			JLabel name = new JLabel("" + weapons.get(i).name); //the name of the weapon
			name.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 10));
			p.add(name);

			JLabel dmg = new JLabel("Shot damage: " + weapons.get(i).damage); //the weapon damage
			dmg.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 10));
			p.add(dmg);

			JLabel hit = new JLabel("Hit chance: " + weapons.get(i).hitRate); //the hit rate of the weapon
			hit.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 10));
			p.add(hit);

			JLabel ap = new JLabel("Action points: " + weapons.get(i).actionPoint); //the action points used when firing the weapon
			ap.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 10));
			p.add(ap);

			JLabel shots = new JLabel("Shots fired: " + weapons.get(i).numAttacks); //the number of time the weapon shoots
			shots.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 10));
			p.add(shots);

			txt.add(spacing);
			txt.add(p);


			weaponButton.add(buttons.get(i), new Integer(0));
			weaponButton.add(txt, new Integer(1));
			this.add(weaponButton);
		}//for


		for(int i = weapons.size(); i < 5; i++)
			this.add(new JLabel(new ImageIcon(normalButtonPath)));
	}//Ship constructor
}//WeaponsPanel